<template>
    <div class="add-address">
        <mt-field placeholder="收货人" v-model="username" class="person-cell"></mt-field>
        <mt-field placeholder="手机号码" type="tel" v-model="phone" class="person-cell"></mt-field>
        <mt-field placeholder="所在城市" v-model="city" class="person-cell"></mt-field>
        <mt-field placeholder="详细地址：街道、门牌号码等" type="textarea" rows="4" v-model="introduction" class="person-cell"></mt-field>
        <mt-cell title="设置默认地址" style="margin-top:.2rem;">
             <mt-switch v-model="value" @change="changeValue"></mt-switch>
        </mt-cell>
        <div class="save-address" @click="saveAddress">保存</div>
    </div>
</template>
<script>
import { post } from '../../../../util/http.js'
import { Toast } from 'mint-ui'
export default {
    data() {
        return {
            username: '',
            phone: '',
            city: '',
            introduction: '',
            default: 1,
            value: false
        }
    },
    methods: {
        changeValue() {
            if (this.value) {
                this.default = 1
            } else {
                this.default = 0
            }
        },
        saveAddress() {
            let info = JSON.parse(window.localStorage.getItem('dongHang_data'))
            if( this.username!='' && this.phone!= ''&& this.city!= '' && this.introduction!= '') {
            let res = this.checkPhone(this.phone)
            if(res) {
                 post('http://dlsapi.rz158.com/address/dealReceiveAddress', {
                    agentId: info.agentId,
                    name: this.username,
                    telephone: this.phone,
                    city: this.city,
                    detailAddress: this.introduction,
                    isDefault: this.default
                }).then(res => {
                    if(res.code == 0) {
                        Toast({
                            message: res.message,
                            duration: 1000
                        })
                        this.$router.push('/my_address')   //操作成功
                    } else {
                        Toast({
                            message: res.message,
                            duration: 1000
                        })
                    }
                })
            }else {
                  Toast({
                        message: '请输入正确手机号',
                        duration: 1000
                    })
            }
         } else {
              Toast({
                        message: '请将信息填写完整',
                        duration: 1000
                    })
         }
        },
        checkPhone (val) {
            let isPhone = /^(13|14|15|16|17|18|19)[0-9]{9}$/.test(val) ? 1 : 0
            if (isPhone === 1) {
            return {
                stat: 1
             }
            }
        }
    }
}
</script>

<style lang="less" scoped>
.add-address{
    background: #efefef;
    height: 13.34rem;
}
.save-address{
    position: absolute;
    font-size: .32rem;
    color: #ffffff;
    background: #29abae;
    height: .82rem;
    width: 100%;
    z-index: 99;
    bottom: 0rem;
    text-align: center;
    line-height: .82rem;
}
 .person-cell {
    border-bottom: 1px solid #cccccc;
}
</style>

